package com.glsc.ngateway.common.base.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
import reactor.util.annotation.Nullable;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Objects;

@Getter
@AllArgsConstructor
public enum ItNetworkAssetsDictFieldTypeEnum {
    STRING("string", "字符", (v, l)->{
        if(Objects.nonNull(l) && StringUtils.isNotBlank(v) && v.length()>l){
            throw new RuntimeException(String.format("\"%s\"超过规定长度:%s", v, l));
        }
    }),
    INT("int", "数值", (v, l)->{
        if(StringUtils.isBlank(v)){
            throw new RuntimeException(String.format("值不能为空"));
        }
        if(!v.matches("\\d+") || v.charAt(0) == '0'){
            throw new RuntimeException(String.format("\"%s\"不合法", v));
        }
    }),
    DATA("date", "日期", (v, l)->{
        //暂时不检查
    })
    ;

    private final String code;
    private final String desc;
    private final ItNetworkAssetsDictFieldCheckMothod checkMethod;

    public static ItNetworkAssetsDictFieldTypeEnum valuesOf(String code){
        return Arrays.stream(ItNetworkAssetsDictFieldTypeEnum.values())
                .filter(t->t.getCode().equals(code))
                .findFirst()
                .orElseThrow(()->new RuntimeException(String.format("未匹配到网络资产字典字段类型:%s", code)));
    }
    @FunctionalInterface
    public interface ItNetworkAssetsDictFieldCheckMothod {
        /**
         * 检查字典字段值
         * @param value 字段值
         * @param len   字段要求长度
         */
        void check(@NotBlank String value, @Nullable Integer len);
    }
}
